package com.gears_software.sxdc.dccx.ui.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IMemberFinder;
import com.gears_software.sxdc.dccx.data.finder.IVehicleFinder;
import com.gears_software.sxdc.dccx.data.model.MemberModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.VehicleModel;
import com.gears_software.sxdc.dccx.ui.activity.AddCarInfoActivity_;
import com.gears_software.sxdc.dccx.ui.activity.JiaShiZhengActivity_;
import com.gears_software.sxdc.dccx.ui.activity.MyCarActivity;
import com.gears_software.sxdc.dccx.ui.activity.XingShiZhengActivity_;
import com.gears_software.sxdc.dccx.ui.activity.XingShiZhengAuthenticationActivity_;
import com.j256.ormlite.stmt.query.In;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

/**
 * Created by chezhenzhen on 2017/7/26.
 */

@EViewGroup(R.layout.item_view_mycar)
public class MyCarListItemView extends LinearLayout {

    Context context;
    @App
    DCCXApplication app;
    @ViewById
    TextView txt_mycar_icon;
    @ViewById
    TextView txt_modify_icon;
    @ViewById
    TextView txt_delete_icon;
    @ViewById
    TextView txt_mycar_renzheng;
    @ViewById
    TextView txt_mycar_moren;
    @ViewById
    LinearLayout ly_modify;
    @ViewById
    LinearLayout ly_renzheng;
    @ViewById
    TextView txt_mycar_type;
    @ViewById
    TextView txt_carcolor;
    @ViewById
    TextView txt_carno;
    @ViewById
    TextView txt_state;
    @ViewById
    RelativeLayout re_current_car;
    @ViewById
    LinearLayout ly_moren;
    @ViewById
    TextView txt_current_car_icon;
    @RestService
    IVehicleFinder vehicleFinder;
    VehicleModel model;

    int position;

    public MyCarListItemView(Context context) {
        super(context);
        this.context = context;
    }

    public void bind(VehicleModel model, int position) {
        this.model = model;
        this.position = position;
        txt_mycar_icon.setTypeface(app.iconfont);
        txt_modify_icon.setTypeface(app.iconfont);
        txt_mycar_renzheng.setTypeface(app.iconfont);
        txt_delete_icon.setTypeface(app.iconfont);
        txt_mycar_moren.setTypeface(app.iconfont);
        txt_current_car_icon.setTypeface(app.iconfont);
        if (model != null) {
            txt_mycar_type.setText(model.getCarmodel());
            txt_carcolor.setText(model.getVehiclecolor());
            txt_carno.setText(model.getVehicleno());
            if (model.getIsdefault() != null && !model.getIsdefault().equals("")) {
                if (model.getIsdefault().equals("1")) {
                    re_current_car.setVisibility(VISIBLE);
                    ly_moren.setVisibility(GONE);
                } else {
                    re_current_car.setVisibility(GONE);
                    ly_moren.setVisibility(VISIBLE);
                }
            }
            switch (model.getCarauditstate()) {
                case "1":
                    txt_state.setText("末认证");
                    txt_state.setTextColor(ContextCompat.getColor(context, R.color.colorFontGray));
                    txt_state.setBackgroundResource(R.drawable.rounded_gray_with_white);
                    break;
                case "2":
                    txt_state.setText("认证中");
                    txt_state.setTextColor(ContextCompat.getColor(context, R.color.colorFontOrange));
                    txt_state.setBackgroundResource(R.drawable.rounded_rectangle_orange);
                    break;
                case "3":
                    txt_state.setText("认证失败");
                    txt_state.setTextColor(ContextCompat.getColor(context, R.color.colorActionbar));
                    txt_state.setBackgroundResource(R.drawable.rounded_rectangle_red);
                    break;
                case "4":
                    txt_state.setText("已认证");
                    txt_state.setTextColor(ContextCompat.getColor(context, R.color.colorGreen));
                    txt_state.setBackgroundResource(R.drawable.rounded_rectangle_green);
                    break;
            }
        }

    }

    @Click
    void ly_renzheng() {
        switch (model.getCarauditstate()) {
            case "1":
                goXSZ();
                break;
            case "2":
                goRZ();
                break;
            case "3":
                goXSZ();
                break;
            case "4":
                goRZ();
                break;
        }
    }

    @Click
    void ly_modify() {
        switch (model.getCarauditstate()) {
            case "1":
                updateModifyCarInfo();
                break;
            case "2":
                showDialog("车辆信息正在认证中，不得进行修改");
                break;
            case "3":
                updateModifyCarInfo();
                break;
            case "4":
                showDialog("车辆信息认证成功，不得进行修改");
                break;
        }

    }

    private void updateModifyCarInfo() {
        Intent intent = new Intent(context, AddCarInfoActivity_.class);
        intent.putExtra("title", "修改车辆");
        intent.putExtra("flag", "modify");
        intent.putExtra("vehicleModel", model);
        ((MyCarActivity) context).startActivityForResult(intent, 1);
    }

    private void showDialog(String title) {
        new MaterialDialog.Builder(context)
                .title("提示")
                .titleGravity(GravityEnum.CENTER)
                .titleColorRes(R.color.colorFontGray)
                .content(title)
                .contentColorRes(R.color.black)
                .positiveText("确定")
                .positiveColor(ContextCompat.getColor(context, R.color.colorFontBlue))
                .buttonsGravity(GravityEnum.CENTER)
                .show();
    }

    @Click
    void ly_delete() {
        switch (model.getCarauditstate()) {
            case "1":
                showDeleteDialog("确定删除车辆信息吗？");
                break;
            case "2":
                showDialog("车辆信息认证中，不得删除");
                break;
            case "3":
                showDeleteDialog("确定删除车辆信息吗？");
                break;
            case "4":
                showDialog("车辆信息认证成功，不得删除");
                break;
        }
    }

    private void showDeleteDialog(String title) {
        new MaterialDialog.Builder(context)
                .title("提示")
                .titleGravity(GravityEnum.CENTER)
                .titleColorRes(R.color.colorFontGray)
                .content(title)
                .contentColorRes(R.color.black)
                .positiveText("确定")
                .positiveColor(ContextCompat.getColor(context, R.color.colorFontBlue))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        deleteVehicle();
                    }
                })
                .negativeText("取消")
                .negativeColor(ContextCompat.getColor(context, R.color.colorFontGray))
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                })
                .buttonsGravity(GravityEnum.CENTER)
                .show();
    }

    @Background
    void deleteVehicle() {
        ResultModel<String> result = vehicleFinder.deleteVehicle(model.getId());
        showResult(result);
    }

    @UiThread
    void showResult(ResultModel<String> result) {
        if (result != null) {
            if (result.getErrno().equals("0")) {
                TastyToast.makeText(context, result.getData(), app.SHORT, app.SUCCESS);
                ((MyCarActivity) context).getVehicleList();
            } else {
                TastyToast.makeText(context, result.getErrmsg(), app.SHORT, app.ERROR);
            }
        }
    }

    private void goXSZ() {
        Intent intent = new Intent(context, XingShiZhengActivity_.class);
        intent.putExtra("id", model.getId());
        intent.putExtra("vehiclno",model.getVehicleno());
        intent.putExtra("carmodel",model.getCarmodel());
        ((MyCarActivity) context).startActivityForResult(intent, 1);
    }

    private void goRZ() {
        Intent intent = new Intent(context, XingShiZhengAuthenticationActivity_.class);
        intent.putExtra("id", model.getId());
        context.startActivity(intent);
    }

    @Click
    void ly_moren() {
        setVehicleDefault();
    }

    @Background
    void setVehicleDefault() {
        ResultModel<String> res = vehicleFinder.setVehicleDefault(model.getId());
        showDefaultResult(res);
    }

    @UiThread
    void showDefaultResult(ResultModel<String> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                TastyToast.makeText(context, "设置默认车辆成功", app.SHORT, app.SUCCESS);
                ((MyCarActivity) context).getVehicleList();
            } else {
                TastyToast.makeText(context, "设置默认车辆失败", app.SHORT, app.ERROR);

            }
        }

    }

}
